package com.koron.configure.changeshift.service;

import cn.hutool.core.convert.Convert;
import com.koron.common.core.util.CodeTools;
import com.koron.common.core.util.StringUtils;
import com.koron.configure.changeshift.bean.ChangeShiftCFBean;
import com.koron.configure.changeshift.bean.ChangeShiftCFDto;
import com.koron.configure.changeshift.mapper.ChangeShiftCFMapper;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ChangeShiftCFImpService implements ChangeShiftCFService {


    @Override
    @TaskAnnotation("addChangeShiftCF")
    public String addChangeShiftCF(SessionFactory factory, ChangeShiftCFDto dto) {
        String id = CodeTools.getCode32();
        ChangeShiftCFBean bean = Convert.convert(ChangeShiftCFBean.class, dto);
        bean.setId(id);
        ChangeShiftCFMapper mapper = factory.getMapper(ChangeShiftCFMapper.class);
        int twoqty = mapper.countByFirstLevel(dto.getFirstLevel());
        if(StringUtils.isEmpty(bean.getSort())){
            if (twoqty == 0) {
                twoqty = 1;
            } else {
                twoqty = twoqty + 1;
            }
            bean.setSort(String.valueOf(twoqty));
        }
        mapper.insertSelective(bean);
        return id;
    }

    @Override
    @TaskAnnotation("updateChangeShiftCF")
    public String updateChangeShiftCF(SessionFactory factory, ChangeShiftCFDto dto) {
        ChangeShiftCFBean bean = Convert.convert(ChangeShiftCFBean.class, dto);
        factory.getMapper(ChangeShiftCFMapper.class).updateByPrimaryKeySelective(bean);
        return dto.getId();
    }

    @Override
    @TaskAnnotation("queryList")
    public List<ChangeShiftCFDto> queryList(SessionFactory factory, ChangeShiftCFDto dto) {
        return factory.getMapper(ChangeShiftCFMapper.class).queryAllByDeptIdAndDeptNameAndLocationAndLocationName(dto);
    }
}
